package org.piggy.common.async;

import java.util.Arrays;

public class RequestFuture {

    //每次请求id，id可以设置原子性增长
    private long id;
    private String path;
    private Object request;
    private Object[] param;
    private Object result;

    public Object[] getParam() {
        return param;
    }

    public void setParam(Object[] param) {
        this.param = param;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Object getRequest() {
        return request;
    }

    public void setRequest(Object request) {
        this.request = request;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public RequestFuture(long id) {
        this.id = id;
    }

    public Object waitResult() {
        if(this.result == null) {
            synchronized (this) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return this.result;
    }

    public void receiveResult(Response resp) {
        this.setResult(resp.getResult());
        synchronized (this) {
            this.notify();
        }
    }

    @Override
    public String toString() {
        return "RequestFuture{" +
                "id=" + id +
                ", path='" + path + '\'' +
                ", request=" + request +
                ", param=" + Arrays.toString(param) +
                ", result=" + result +
                '}';
    }
}
